package com.ydh.sync;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import weaver.interfaces.schedule.BaseCronJob;

public class TestPointLogin extends BaseCronJob {

    @Override
    public void execute() {
        // TODO Auto-generated method stub
    }

    public void getToken() throws URISyntaxException, IOException {
        BufferedReader in = null;
        String url = "http://127.0.0.1:8089/ssologin/getToken";
        Map<String, String> params = new HashMap<>();
        params.put("appid", "Client1");
        params.put("loginid", "sw");
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost();
        request.setURI(new URI(url));
        // 设置参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String value = String.valueOf(params.get(name));
            nvps.add(new BasicNameValuePair(name, value));
            // System.out.println(name +"-"+value);
        }
        request.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = client.execute(request);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) { // 请求成功
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
        } else { //
            System.out.println("状态码：" + code);
        }
        // 获 得 的 token:
        // 659C43561254716F8961A2586888BD17288178101C86582821E50E190F941A72
    }
}
